<?php
namespace Home\Model;
use Think\Model;

class ArticleModel extends Model {

    public function timeFormat($search,$flag=false) {
        if ($flag) {
            $time = NOW_TIME - $search['pub_time'];
            if ($time < 60) {
                $search['pub_time'] = '刚刚发布';
            } else if ($time < 60 * 60 ) {
                $search['pub_time'] = floor($time / 60).'分钟之前';
            } else if (date('Y-m-d',NOW_TIME) == date('Y-m-d',$search['pub_time'])) {
                $search['pub_time'] = '今天'.date('H:i', $search['pub_time']);
            } else if (date('Y-m-d',strtotime('-1 day')) == date('Y-m-d',$search['pub_time'])) {
                $search['pub_time'] = ' 昨 天 '.date('H:i', $search['pub_time']);
            } else if (date('Y') == date('Y', $search['pub_time'])) {
                $search['pub_time'] = date('m 月 d 日 H:i', $search['pub_time']);
            } else {
                $search['pub_time'] = date('Y 年 m 月 d 日 H:i', $search['pub_time']);
            }
        } else {
            foreach ($search as $key => $value) {
                $time = NOW_TIME - $value['pub_time'];
                if ($time < 60) {
                    $search[$key]['pub_time'] = '刚刚发布';
                } else if ($time < 60 * 60) {
                    $search[$key]['pub_time'] = floor($time / 60) . '分钟之前';
                } else if (date('Y-m-d', NOW_TIME) == date('Y-m-d', $value['pub_time'])) {
                    $search[$key]['pub_time'] = '今天' . date('H:i', $value['pub_time']);
                } else if (date('Y-m-d', strtotime('-1 day')) == date('Y-m-d', $value['pub_time'])) {
                    $search[$key]['pub_time'] = ' 昨 天 ' . date('H:i', $value['pub_time']);
                } else if (date('Y') == date('Y', $value['pub_time'])) {
                    $search[$key]['pub_time'] = date('m 月 d 日 H:i', $value['pub_time']);
                } else {
                    $search[$key]['pub_time'] = date('Y 年 m 月 d 日 H:i', $value['pub_time']);
                }
            }
        }
        return $search;
    }

    public function tag($tag,$flag=false) {

        if ($flag) {
            foreach ($tag as $key => $value) {
                $tag[$key]['art_tag'] = explode(',',$value['art_tag']);
            }
        } else {
            $tag = explode(',',$tag);
        }

        return $tag;
    }

    //最新发布的new 8篇文章
    public function news($page) {
        $new = $this->field('blog_article.*,blog_category.cat_name')
            ->join('blog_category ON blog_article.cat_id = blog_category.cat_id','LEFT')
            ->order('pub_time desc')
            ->limit($page->firstRow.','.$page->listRows)
            ->select();
        return $new;
    }

    //取得一条文章详细内容
    public function detail($id) {
        $map['blog_article.art_id'] =  $id;
        $detail = $this->field('blog_article.*,blog_category.cat_name')
            ->join('blog_category ON blog_article.cat_id = blog_category.cat_id','LEFT')
            ->where($map)
            ->find();
        return $detail;
    }
    //取得文章列表
    public function articleList($Page,$map = 1) {
        $search = $this->field('art_id,art_author,art_view,art_title,art_description,pub_time')
                       ->limit($Page->firstRow.','.$Page->listRows)
                       ->where($map)->select();
        return $search;
    }

    //取得文章列表2
    public function articleList2($Page,$map = 1) {
        $search = $this->field('art_id,art_author,art_view,art_title,art_description,pub_time')
            ->where($map)->select();

      /*  return array(
            'total' => $this->count(),
            'rows' => $search ? $search : ''
        );*/

        return $search;

    }


    //文章查看次数自增
    public function setInc($id) {
        $map['blog_article.art_id'] =  $id;
        //文章查看次数自增
        M('Article')->where($map)->setInc('art_view');

    }
    //获得上篇文章
    public function pre($id) {
        $mp['art_id'] = array('lt',$id);
        $pre = $this->field('art_id,art_title')
            ->order('art_id desc')
            ->where($mp)->find();

        return $pre;
    }

    //获得上篇文章
    public function next($id) {
        //获得下篇文章
        $m['art_id'] = array('gt',$id);
        $next = M('Article')->field('art_id,art_title')
            ->order('art_id asc')
            ->where($m)->find();
        return $next;
    }


    //相关文章
    public function relation($id) {
        $pp['cat_id'] = $id;
        $pp['art_id'] = array('NEQ',I('get.id'));
        $relation =  M('Article')->field('art_id,art_title')
            ->where($pp)
            ->limit(5)->select();
        return $relation;
    }

    //去掉html 标签
    public function htmlTag($tag){
        foreach ($tag as $key => $value) {
            $tag[$key]['new_content'] = mb_substr(strip_tags($value['art_content']),0,300,'utf-8').'...';
        }
        return  $tag;
    }

    //获得栏目的家谱树
    public function getFamily($id=0) {
        $tree = array();
        while($id>0) {
            foreach($this->getAll() as $k=>$v) {
                if($v->cat_id == $id) {
                    $tree[$k]['cat_name'] = $v->cat_name;
                    $tree[$k]['cat_id'] = $v->cat_id;
                    $id = $v['parent_id'];
                    break;
                }
            }
        }
        return array_reverse($tree);
    }

    //文章顶级栏目或者 子栏目文章搜索
    public function search($ids,$page) {
        $search = $this->field('art_id,blog_article.cat_id,cat_name,art_title,art_author,art_tag,pub_time,art_view,art_description')
                        ->join('blog_category ON blog_article.cat_id=blog_category.cat_id','LEFT')
                       ->where($ids)
                       ->limit($page->firstRow.','.$page->listRows)
                       ->select();
        return $search;

    }
}
